<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9/009
 * Time: 11:35
 */

namespace app\api\model;

use app\lib\exception\ArticleException;

class Article extends BaseModel {

    public function getImageAttr($value) {
        return get_img_url($value);
    }

    /**
     * 获取文章列表
     * @return \think\Paginator
     */
    public static function articleList() {
        $article = self::field('image, id, title, hit')
            ->where('status', 1)
            ->where('delete', 0)
            ->order('id desc')
            ->paginate(8, true);

        return $article;
    }

    /**
     * 获取文章详情
     * @param $id
     * @return array|null|\PDOStatement|string|\think\Model
     * @throws ArticleException
     */
    public static function articleInfo($id) {
        $article = self::field('image, id, title, hit, content, video')
            ->where('status', 1)
            ->where('delete', 0)
            ->where('id', $id)
            ->find();
        if (!$article) {
            throw new ArticleException();
        }

        $article->setInc('hit', 1);

        return $article;
    }

}